<template>
  <div id="roleTable">
    <div class="table">
      <el-form :model="roleForm" :rules="rules" ref="roleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色类型" prop="roleExtent">
          <el-input v-model="roleForm.roleType"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <BMSIcon v-model="roleForm.icon"/>
        </el-form-item>
        <el-form-item label="菜单"> 
          <div class="roleMenu">
            <el-tree
              ref="tree"
              default-expand-all
              :data="data"
              show-checkbox
              :default-checked-keys="roleForm.roleMenuIdList"
              node-key="id">
            </el-tree>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('roleForm')">提交</el-button>
          <el-button @click="resetForm('roleForm')">重置</el-button>
          <el-button @click="returnForm()">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  getMenuTree,checkRoleName
} from '@/assets/request/api.js';
import BMSIcon from '@/components/component/common/bmsIcon.vue';
export default {
  name: 'roleTable',
  props:[ 'content' ],
  components: { BMSIcon },
  data() {
    var validateRoleName = (rule, value, callback) => {
      if(value === '') {
        callback(new Error("请输入角色名称"))
      }
      let id = ""
      if(this.operation !== 'insert') {
        id = this.roleForm.id
      }
      checkRoleName(value,id).then(res => {
        callback()
      }).catch(err => {
        callback(new Error(err.message))
      })
    };
    return {
      operation: 'insert',
      data: [],
      roleForm: {
        name:'',
        roleType:'',
        icon:'',
        roleMenuIdList: [],        // 选择的列表
      },
      rules: {
        name: [
          { required: true, validator: validateRoleName, trigger: 'blur' },
        ],
        icon: [
          { required: true, message: '请输入图标', trigger: 'blur' },
        ]
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.roleForm.roleMenuIdList = this.$refs.tree.getCheckedKeys()
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 将内容 返回给 父组件， 让他自己去做
          this.$emit("submitRole",this.roleForm);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs.tree.setCheckedKeys([]);
      this.$refs[formName].resetFields();
    },
    returnForm() {
      this.$router.go(-1)
    },
    async initMenuTree() {
      await getMenuTree().then(res => {
        this.data = res.data
      }).catch(err => {
        this.$message({
          message: err.message,
          type: 'error'
        });
      })
    }
  },
  created() {
    this.initMenuTree()
  },
  watch: {
    content : {
      handler(newValue) {
        this.roleForm = newValue
        this.operation = 'update'
      }
    }
  }
}
</script>

<style scoped lang="scss">

.roleMenu {
  line-height: 24px;
  padding: 10px;
}

#roleTable {
  display: flex;
	justify-content: center;
	align-items: center;
  .table {
    margin-top: 20px;
    display: inline-block;
  }
}

.el-button {
  line-height: 0px;
  height: 32px;
  padding: 12px;
  font-size: 2px;
  border-radius: 20px;
}

.el-input , .el-select { 

  /deep/ .el-input__inner {
    height: 32px;
    font-size: 13px;
    box-shadow: none;
    width: 400px;
    border: 1px solid #e9e9e9;
    border-radius: 20px;
  }

}

</style>
